package com.toy.core.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.toy.core.cache.EntityCacheManager;
import com.toy.core.cache.FinderPath;
import com.toy.core.dao.MongoCrudRepository;
import com.toy.core.dao.support.Page;
import com.toy.core.domain.BaseObject;

@Deprecated
public abstract class SimpleManager<T extends BaseObject, ID extends Serializable> {
	  
	@Autowired
	private EntityCacheManager entityCacheManager;
	
	/*---------------------save方法---------------------*/
	public T saveOrUpdate(T entity) {
		T t = getMongoCrudRepository().save(entity);
		entityCacheManager.clearCache(entity.getClass().getName());
		return t;
	}

	public T updateById(ID id, String propertyName,
			Object newValue) {
		T t = getMongoCrudRepository().update(id, propertyName, newValue);
		entityCacheManager.clearCache(getMongoCrudRepository().getEntityClass().getName());
		return t;
	}
	

	
	/*---------------------删除方法---------------------*/
	public void delete(ID id){
		getMongoCrudRepository().delete(id);
		entityCacheManager.clearCache(getMongoCrudRepository().getEntityClass().getName());
	}
	
	public void delete(T entity){
		getMongoCrudRepository().delete(entity);
		entityCacheManager.clearCache(entity.getClass().getName());
	}
	
	/*---------------------get方法---------------------*/
	@SuppressWarnings("unchecked")
	public T getById(ID id) {
		FinderPath finderPath = new FinderPath(getMongoCrudRepository().getEntityClass().getName(),"getById",new String[]{id.getClass().getName()});
		Object[] finderArgs =  new Object[]{id};
		
		Object entity = entityCacheManager.findCache(finderPath, finderArgs);
		if ( entity == null ){
			T t = getMongoCrudRepository().getById(id);
			entityCacheManager.putCache(finderPath, finderArgs, t);
			return t;
		}
		return (T) entity;
	}
	
	@SuppressWarnings("unchecked")
	public T getUniqueByProperty(String propertyName, Object value) {
		FinderPath finderPath = new FinderPath(getMongoCrudRepository().getEntityClass().getName(),
				"getUniqueByProperty",new String[]{String.class.getName(),Object.class.getName()});
		Object[] finderArgs =  new Object[]{propertyName,value};
		
		Object entity = entityCacheManager.findCache(finderPath, finderArgs);
		if ( entity == null ){
			T t = getMongoCrudRepository().getUniqueByProperty(propertyName,value);
			entityCacheManager.putCache(finderPath, finderArgs, t);
			return t;
		}
		return (T) entity;
	}
	
	/*---------------------find方法---------------------*/
	@SuppressWarnings("unchecked")
	public List<T> findByProperty(String propertyName,Object value){
		FinderPath finderPath = new FinderPath(getMongoCrudRepository().getEntityClass().getName(),
				"findByProperty",new String[]{String.class.getName(),Object.class.getName()});
		Object[] finderArgs =  new Object[]{propertyName,value};
		Object entity = entityCacheManager.findCache(finderPath, finderArgs);
		if ( entity == null ){
			List<T> results = getMongoCrudRepository().findByProperty(propertyName, value);
			entityCacheManager.putCache(finderPath, finderArgs, results);
			return results;
		}
		return (List<T>)entity;
	}
	
	/*----------------------翻页方法--------------------*/
	@SuppressWarnings("unchecked")
	public Page<T> findPaged(int pageNo, int pageSize){
		FinderPath finderPath = new FinderPath(getMongoCrudRepository().getEntityClass().getName(),
				"findPaged",new String[]{Integer.class.getName(),Integer.class.getName()});
		Object[] finderArgs =  new Object[]{pageNo,pageSize};
		Object entity = entityCacheManager.findCache(finderPath, finderArgs);
		if ( entity == null ){
			Page<T> page = getMongoCrudRepository().findPaged(pageNo, pageSize);
			entityCacheManager.putCache(finderPath, finderArgs, page);
			return page;
		}
		return (Page<T>)entity;
	}
	
	public abstract MongoCrudRepository<T,ID> getMongoCrudRepository();

}
